<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="人员名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入人员名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="归属区域" prop="regionId">
          <el-select v-model="queryParams.regionId" placeholder="请选择区域" clearable>
            <el-option v-for="item in regionList" :key="item.id" :label="item.regionName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleId">
          <el-select v-model="queryParams.roleId" placeholder="请选择角色" clearable>
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item label="员工状态" prop="status">
        <el-radio-group v-model="queryParams.status" @change="handleQuery">
          <el-radio :label="null">全部</el-radio>
          <el-radio v-for="dict in emp_status" :key="dict.value" :label="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      </el-form>
  
      <el-table v-loading="loading" :data="empPerformanceList">
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="人员名称" align="center" prop="userName" />
      <el-table-column label="所属区域" align="center" prop="regionName" />
      <el-table-column label="角色名称" align="center" prop="roleName" />
      <el-table-column label="联系电话" align="center" prop="mobile" />
      <el-table-column label="本月完成工单" align="center" prop="completedTasks">
        <template #default="scope">
          <span :style="getTaskStyle(scope.row.completedTasks, 4)">{{ scope.row.completedTasks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月进行工单" align="center" prop="ongoingTasks">
        <template #default="scope">
          <span :style="getTaskStyle(scope.row.ongoingTasks, 2)">{{ scope.row.ongoingTasks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月待办工单" align="center" prop="pendingTasks">
        <template #default="scope">
          <span :style="getTaskStyle(scope.row.pendingTasks, 1)">{{ scope.row.pendingTasks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月取消工单" align="center" prop="cancelledTasks">
        <template #default="scope">
          <span :style="getTaskStyle(scope.row.cancelledTasks, 3)">{{ scope.row.cancelledTasks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月创建工单" align="center" prop="createdTasks">
        <template #default="scope">
          <span :style="getTaskStyle(scope.row.createdTasks, 'created')">{{ scope.row.createdTasks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工状态" align="center" prop="status">
        <template #default="scope">
          <span :style="getStatusStyle(scope.row.status)">
            <dict-tag :options="emp_status" :value="scope.row.status"/>
          </span>
        </template>
      </el-table-column>
    </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </template>
  
  <script setup name="EmpPerformance">
  import { ref, reactive, toRefs, onMounted } from 'vue';
  import { listEmpPerformance } from "@/api/manage/task";
  import { listRole } from "@/api/manage/role";
  import { listRegion } from "@/api/manage/region";
  
  const { proxy } = getCurrentInstance();
  const { emp_status } = proxy.useDict('emp_status');
  
  const empPerformanceList = ref([]);
  const loading = ref(true);
  const showSearch = ref(true);
  const total = ref(0);
  const roleList = ref([]);
  const regionList = ref([]);
  
  const data = reactive({
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      userName: null,
      regionId: null,
      roleId: null,
    },
  });
  
  const { queryParams } = toRefs(data);
  
  /** 查询员工业绩列表 */
  function getList() {
    loading.value = true;
    listEmpPerformance(queryParams.value).then(response => {
      empPerformanceList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  /** 查询角色列表 */
  function getRoleList() {
    listRole().then(response => {
      roleList.value = response.rows;
    });
  }
  
  /** 查询区域列表 */
  function getRegionList() {
    listRegion().then(response => {
      regionList.value = response.rows;
    });
  }

  const getTaskStyle = (value, status) => {
  if (value === '-') return {};
  
  switch (status) {
    case 1: // 待办
      return { color: '#FF6347', fontWeight: 'bold' , fontSize: '1.2em'};
    case 2: // 进行
      return { color: '#0393EA', fontWeight: 'bold' , fontSize: '1.2em'};
    case 3: // 取消
      return { color: '#FFD700', fontWeight: 'bold' , fontSize: '1.2em'};
    case 4: // 完成
      return { color: '#32CD32', fontWeight: 'bold' , fontSize: '1.2em'};
    case 'created': // 创建工单
      return { color: '#8E42D0', fontWeight: 'bold' , fontSize: '1.2em'};
    default:
      return {};
  }
};

const getStatusStyle = (status) => {
  switch (status) {
    case 0: // 启用
      return { color: '#32CD32', fontWeight: 'bold' };
    case 1: // 停用
      return { color: '#FF6347', fontWeight: 'bold' };
    default:
      return {};
  }
};
  
  onMounted(() => {
    getList();
    getRoleList();
    getRegionList();
  });
  </script>